<meta charset="UTF-8">
<form >
    <p><input placeholder="姓名" id="sname" /></p>
    <p><input placeholder="年龄" id="age" /></p>
    <p><input placeholder="学历" id="xueli" /></p>
    <p><input placeholder="专业" id="zhuanye" /></p>
    <p><input type="button" value="新增学员" id="addbtn"  /></p>
</form>
<script>
    function Student(sname,age,xueli,zhuanye){
        this.name = sname;
        this.age = age;
        this.xueli = xueli;
        this.zhuanye = zhuanye;
    }

    var students = [];

    if(localStorage.students){
        students = JSON.parse(localStorage.students);
       for(var i=0;i<students.length;i++){
           var s = students[i];
           var newp = document.createElement("p");
           newp.innerHTML = s.name+" "+s.age+" "+
               s.xueli+" "+s.zhuanye;
           document.body.appendChild(newp);
       }
    }else{
        console.log("students is null")
    }

    addbtn.onclick = function(){
        var s = new Student(sname.value,age.value,
        xueli.value,zhuanye.value);
        console.log(s);

        var newp = document.createElement("p");
        newp.innerHTML = s.name+" "+s.age+" "+s.xueli+" "+s.zhuanye;
        document.body.appendChild(newp);

        students.push(s);
        localStorage.students = JSON.stringify(students);
    };
</script>






